<template>
	<view class="pt-sale">
		<block v-if="showList.length">
			<swiper class="pt-sale-swiper" :indicator-dots="false" :autoplay="false" :style="{height: swiperHeight + 'px'}" @change="swiperChange">
				<swiper-item v-for="(data,index) in showList" :key="index">
					<view class="pt-sale-list">
						<view class="pt-sale-item" v-for="(item,i) in data" :key="i" @click="goProInfo(item.goods_id,item.goods_type)">
							<view class="pt-sale-item-image"><image :src="item.image" mode="aspectFit"></image></view>
							<view class="pt-sale-item-goods-name">{{item.goods_name}}</view>
							<view class="pt-sale-item-goods-price" v-if="isLogin">
								<text class="retail-price"><text>￥</text>{{item.retail_price}}</text>
								<text class="market-price"><text>￥</text>{{item.market_price?item.market_price:0}}</text>
							</view>
							<view class="noLogin" v-else>登录查看会员价</view>
						</view>
					</view>
				</swiper-item>
			</swiper>
			<view class="pt-sale-scroll-box">
				<view class="pt-sale-scroll-bar" 
				:style="{width: parseInt(100 / showList.length) + '%',left: parseInt(swiperIndex * 100 / showList.length) + '%'}"></view>
			</view>
		</block>
		<pt-nothing :text="'暂无'+name+'商品'" bgColor="#FFF" style="margin-top: -30rpx;" v-else></pt-nothing>
	</view>
</template>

<script>
	export default {
		props: {
			isLogin: {
				type: Boolean,
				default: false
			},
			name: {
				type: String,
				default: ''
			},
			list: {
				type: Array,
				default(){
					return []
				}
			}
		},
		data() {
			return {
				showList: [],
				swiperHeight: 0,
				swiperIndex: 0,
				swiperAllHeight: []
			};
		},
		methods: {
			swiperChange(e){
				this.swiperIndex = e.detail.current
				this.swiperHeight = this.swiperAllHeight[e.detail.current].height
			},
			goProInfo(goods_id,goods_type){
				this.$emit('GoInfoAddFoot')
				switch (goods_type){
					case 1:
						getApp().goProInfo(goods_id)
						break;
				}
			},
			getHeight(){
				setTimeout(() => {
					uni.createSelectorQuery().in(this).selectAll('.pt-sale-list').boundingClientRect().exec(res => {
						this.swiperHeight = res[0][0].height
						this.swiperAllHeight = res[0]
					})
				},500)
			}
		},
		computed: {
			width(){
				let styleObj = {
					width: parseInt(100 / this.showList.length) + '%',
					left: parseInt(this.swiperIndex * 100 / this.showList.length) + '%'
				}
				return styleObj
			}
		},
		mounted() {
			for(var i=0,len=this.list.length;i<len;i+=6){
				this.showList.push(this.list.slice(i,i+6));
			}
			this.$nextTick(() => {
				if(this.showList.length){
					this.getHeight()
				}
			})
		}
	}
</script>

<style lang="scss">
	.pt-sale{
		padding: 0 30rpx;
		.pt-sale-swiper{
			transition: all .2s;
		}
		.pt-sale-list{
			display: flex;
			flex-wrap: wrap;
			.pt-sale-item{
				width: 210rpx;
				margin-bottom: 40rpx;
				margin-right: 30rpx;
				&:nth-child(3n){
					margin-right: 0;
				}
				.pt-sale-item-image{
					image{
						width: 210rpx;
						height: 210rpx;
						border-radius: 8rpx;
						border: solid 2rpx #f7f7f7;
					}
				}
				.pt-sale-item-goods-name{
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
					font-size: 24rpx;
					color: #28221F;
					margin: 20rpx 0;
				}
				.pt-sale-item-goods-price{
					.retail-price{
						color: #F59359;
						font-size: 28rpx;
						font-weight: bold;
						text{
							font-size: 22rpx;
							font-weight: normal;
						}
					}
					.market-price{
						color: #909090;
						font-size: 22rpx;
						text-decoration: line-through;
						margin-left: 10rpx;
					}
				}
				.noLogin{
					display: inline-block;
					background-color: #F59359;
					color: #FFF;
					border-radius: 10rpx;
					padding: 6rpx 12rpx;
					font-size: 20rpx;
				}
			}
		}
		.pt-sale-scroll-box{
			width: 80rpx;
			height: 6rpx;
			background-color: #E6E6E6;
			border-radius: 10rpx;
			margin: 0 auto;
			position: relative;
			overflow: hidden;
			.pt-sale-scroll-bar{
				position: absolute;
				left: 0;
				top: 0;
				height: 100%;
				background-color: #F59359;
				transition: all .2s;
				border-radius: 10rpx;
			}
		}
	}
</style>